package communityController;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import vo.CommunityVo;
import dao.CommunityDao;

@Controller
public class CommunityBoardReply {
	
	@Autowired
	CommunityDao cdao;
	
	@Autowired
	HttpServletRequest request;

	@RequestMapping(value="/project/community/reply_write.do")
	public ModelAndView exe(CommunityVo cvo) throws Exception {
		
		System.out.println("no:"+cvo.getB_no());
		System.out.println("menu_no:"+cvo.getMenu_no());
		System.out.println("cate_no:"+cvo.getCate_no());
		System.out.println("content:"+cvo.getB_content());
		
		String id = (String)request.getSession().getAttribute("id");
		int count = cdao.selectReplyCount(cvo.getB_no());
		String ip = request.getRemoteAddr();
		cvo.setB_ip(ip);
		cvo.setB_writer(id);
		cvo.setB_ref(cvo.getB_no());
		cvo.setB_step(count+1);
		cdao.replyWrite(cvo);
		
		Map<String,Integer> map= new HashMap<String,Integer>();
		map.put("b_reply_count", cvo.getB_step());
		map.put("b_no", cvo.getB_no());
		
		cdao.updateReplyCount(map);
		
		ModelAndView mv = new ModelAndView();
		//mv.addObject("menu_no",cvo.getMenu_no());
		mv.addObject("cate_no",cvo.getCate_no());
		mv.addObject("b_no", cvo.getB_no());
		mv.setViewName("redirect:board_view.do");
		return mv;
	}

}
